/**************************************************************************************
 *  Copyright (c)  2010  Yan Santos, Leszek Karcz.
 *  Permission is granted to copy, distribute and/or modify this document
 *  under the terms of the GNU Free Documentation License, Version 1.3
 *  or any later version published by the Free Software Foundation;
 *  with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 *  A copy of the license is included in the section entitled "GNU
 *  Free Documentation License". 
 **************************************************************************************/
package data;

/**************************************************************************************
 * Author(s): Yan Santos, Leszek Karcz
 * this class keeps track of used and free IDs for player controllables
 **************************************************************************************/
public class IDTable
{
	private boolean[] taken;
	private int capacity;

	/**************************************************************************************
	 * Author(s): Yan Santos
	 * IDTable constructor
	 **************************************************************************************/
	public IDTable(int capacity) {
		this.capacity=capacity;
		this.taken = new boolean[this.capacity];
		for (int i = 0; i < this.capacity; i++)
		{
			this.taken[i] = false;
		}
	}

	/**************************************************************************************
	 * Author(s): Yan Santos
	 * check if ID is free
	 **************************************************************************************/
	public boolean check(int ID)
	{
		return this.taken[ID];
	}
	
	/**************************************************************************************
	 * Author(s): Yan Santos
	 * reserve ID
	 **************************************************************************************/
	public void reserve(int ID)
	{
		this.taken[ID] = true;
	}

	/**************************************************************************************
	 * Author(s): Yan Santos
	 * release ID
	 **************************************************************************************/
	public void release(int ID)
	{
		this.taken[ID] = false;
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * reserves and returns first free ID, returns -1 if all are taken
	 **************************************************************************************/
	public int getFirstFree()
	{
		int freeID=-1;
		for(int i = 0; i < this.capacity; i++)
		{
			if(!this.taken[i])
			{
				freeID=i;
				this.reserve(freeID);
				break;
			}
		}
		return freeID;
	}
}